package com.yhp.dao.impl;

import com.yhp.bean.Menu;
import com.yhp.bean.Role;
import com.yhp.bean.Users;
import com.yhp.dao.MenuDao;
import com.yhp.util.DBUtilsPlus;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends DBUtilsPlus implements MenuDao {
    @Override
    public List<Menu> selectAll() {
        List<Menu> menulist=new ArrayList<Menu>();
        try {
            String sql="select * from menu";
            resultSet = selectDB(sql, null);
            while(resultSet.next()){
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menulist.add(menu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return menulist;
    }

    @Override
    public int deleteById(int id) {
        int i =0;
        try {
            String sql="update menu set status=1 where menuid=?";
            List params=new ArrayList();
            params.add(id);
            i = updateDB(sql, params);//i 表示受影响的行数
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }

    @Override
    public int updateMenu(Menu menu) {
        int i =0;
        try {
            String sql="update menu set menuname=? where menuid=?";
            List params=new ArrayList();
            params.add(menu.getMenuName());
            params.add(menu.getMenuId());
            i = updateDB(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }

    @Override
    public int insertMenu(Menu menu) {
        int i = 0;
        try {
            String sql="insert into menu(menuid,menuname) values(null,?)";
            List params=new ArrayList();
            params.add(menu.getMenuName());
            i = updateDB(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }

    @Override
    public Menu findById(int id) {
        Menu menu=null;
        try {
            String sql="select * from menu where menuid=?";
            List params=new ArrayList();
            params.add(id);
            resultSet = selectDB(sql, params);
            while(resultSet.next()){
                menu=new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return menu;
    }
}
